﻿#ifndef MYTCPSOCKET_H
#define MYTCPSOCKET_H

#include <QObject>
#include <QThread>
#include <QMutex>
#include <QTcpSocket>
#include <QAbstractSocket>
#include <memory>

#include "NetHeader.h"

class MyTcpSocket : public QThread
{
    Q_OBJECT
public:
    explicit MyTcpSocket(QObject *parent = nullptr);
    ~MyTcpSocket();

    bool connectToServer();

    QString errorString();

    void disconnectFromHost();

    quint32 getLocalIp();

public slots:
    void recvToSocket();

    void StopTcpThread();

    void errorDetect(QAbstractSocket::SocketError error);

signals:
    void socketError(QAbstractSocket::SocketError);

private:
    QTcpSocket      *m_tcpSocket;
    QThread         *m_tcpSocketThread;
    QMutex          m_mutex;
    uchar           *m_sendBuff;
    uchar           *m_recvBuff;
    quint64         m_recvBuffBytes;
    bool            m_isRun;

    void run() override;

private slots:
    bool connectServer();

    void sendData(MESG* msg);

    void closeSocket();
};

#endif // MYTCPSOCKET_H
